package com.rgst.controller;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rgst.anno.DontLogin;
import com.rgst.dao.entity.User;
import com.rgst.dao.repository.UserRepository;
import com.rgst.utils.Md5SaltTool;
import com.rgst.vo.RgstUserVo;

/**
 * Created by tancw on 2019/11/20.
 */
@Controller
@RequestMapping("rgst")
public class RgstController extends BaseController{

    @Autowired
    private UserRepository userRepository;

    @PostMapping("addUser")
    @DontLogin
    public ModelAndView rgst(@Valid RgstUserVo rgstUserVo){
        ModelAndView view = new ModelAndView();
        try {
            User user = new User();
            user.setRealname(rgstUserVo.getRealname());
            user.setIdcard(rgstUserVo.getIdno());
            user.setSex(rgstUserVo.getSex());
            user.setAddress(rgstUserVo.getAddress());
            user.setPassword(Md5SaltTool.getEncryptedPwd(rgstUserVo.getPassword()));
            user.setMobile(rgstUserVo.getMobile());
            user.setStatus(1);
            user.setCreateTime(new Date());
            userRepository.save(user);
            view.setViewName("success");
        }catch (Exception e){
           e.printStackTrace();
           view.setViewName("error");
           view.addObject("message",e.getMessage());
        }
        return view;
    }

}
